{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Largest Perimeter Triangle"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #greedy #array #math #sorting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #贪心 #数组 #数学 #排序"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: largestPerimeter"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #三角形的最大周长"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给定由一些正数（代表长度）组成的数组 <code>nums</code>&nbsp;，返回 <em>由其中三个长度组成的、<strong>面积不为零</strong>的三角形的最大周长</em>&nbsp;。如果不能形成任何面积不为零的三角形，返回&nbsp;<code>0</code>。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<ol>\n",
    "</ol>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>nums = [2,1,2]\n",
    "<strong>输出：</strong>5\n",
    "<strong>解释：</strong>你可以用三个边长组成一个三角形:1 2 2。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>nums = [1,2,1,10]\n",
    "<strong>输出：</strong>0\n",
    "<strong>解释：</strong>\n",
    "你不能用边长 1,1,2 来组成三角形。\n",
    "不能用边长 1,1,10 来构成三角形。\n",
    "不能用边长 1、2 和 10 来构成三角形。\n",
    "因为我们不能用任何三条边长来构成一个非零面积的三角形，所以我们返回 0。</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>3 &lt;= nums.length &lt;= 10<sup>4</sup></code></li>\n",
    "\t<li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [largest-perimeter-triangle](https://leetcode.cn/problems/largest-perimeter-triangle/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [largest-perimeter-triangle](https://leetcode.cn/problems/largest-perimeter-triangle/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[2,1,2]', '[1,2,1,10]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestPerimeter(self, nums: List[int]) -> int:\n",
    "        nums = sorted(nums, reverse = True)\n",
    "        i = 0\n",
    "        \n",
    "        while i < len(nums)-2:\n",
    "            if nums[i+1] + nums[i+2] > nums[i]:\n",
    "                return nums[i] + nums[i+1] + nums[i+2]\n",
    "            else:\n",
    "                i += 1\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestPerimeter(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        list_len = len(nums)\n",
    "        if list_len < 3:\n",
    "            return 0\n",
    "        i = list_len - 3\n",
    "        while i >= 0:\n",
    "            a = nums[list_len-1]\n",
    "            b = nums[list_len-2]\n",
    "            c = nums[i]\n",
    "            sort_list = [a,b,c]\n",
    "            sort_list.sort()\n",
    "            if sort_list[0] + sort_list[1] > sort_list[2] and sort_list[2] - sort_list[1] < sort_list[0]:\n",
    "                return a + b + c                \n",
    "            i -= 1\n",
    "        \n",
    "        return self.largestPerimeter(nums[0:list_len-1])\n",
    "\n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestPerimeter(self, nums: List[int]) -> int:\n",
    "        nums.sort(key=None,reverse=True)\n",
    "        count=0\n",
    "        for i in range(0,len(nums)-2):\n",
    "            if nums[i+1]+nums[i+2]>nums[i]:\n",
    "                count=nums[i]+nums[i+1]+nums[i+2]\n",
    "                return count\n",
    "        return count\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestPerimeter(self, nums: List[int]) -> int:\n",
    "        l = nums\n",
    "        l.sort(reverse=True)\n",
    "        index = 0\n",
    "        for i in range(len(l)-2):\n",
    "            if l[i]<l[i+1]+l[i+2]:\n",
    "                index = 1\n",
    "                return l[i]+l[i+1]+l[i+2]\n",
    "        if index==0:\n",
    "            return 0\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from audioop import reverse\r\n",
    "\r\n",
    "\r\n",
    "class Solution:\r\n",
    "    def largestPerimeter(self, nums: List[int]) -> int:\r\n",
    "        if len(nums) < 3:\r\n",
    "            return 0\r\n",
    "        if not getattr(self, 'sort', None):\r\n",
    "            nums.sort(reverse=True)\r\n",
    "            self.sort = True\r\n",
    "        if nums[1] + nums[2] > nums[0]:\r\n",
    "            return sum(nums[:3])\r\n",
    "        else:\r\n",
    "            return self.largestPerimeter(nums[1:])\r\n",
    "\r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestPerimeter(self, nums: List[int]) -> int:\n",
    "        nums.sort() #先排序\n",
    "        n = len(nums)\n",
    "        for k in range(n - 2):\n",
    "            if nums[n - 3 - k] + nums[n - 2 -k] > nums[n - k - 1]:\n",
    "                return nums[n - 3 - k] + nums[n - 2 - k] + nums[n - 1 - k]\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestPerimeter(self, A: List[int]) -> int:\n",
    "        A=sorted(A)\n",
    "        def helper(nums):\n",
    "            if len(nums)<3:\n",
    "                return 0\n",
    "            if nums[-1]<nums[-2]+nums[-3]:\n",
    "                return sum(nums[-3:])\n",
    "            return helper(nums[:-1])\n",
    "        return helper(A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestPerimeter(self, nums: List[int]) -> int:\n",
    "        nums.sort(reverse = True)\n",
    "        for i in range(len(nums) - 2) :\n",
    "            if nums[i] < nums[i + 1] + nums[i + 2] :\n",
    "                return nums[i] + nums[i + 1] + nums[i + 2]\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestPerimeter(self, nums: List[int]) -> int:\n",
    "        length=0\n",
    "        nums.sort(reverse=True)\n",
    "        for i in range(2,len(nums)):\n",
    "            a=nums[i]+nums[i-1]+nums[i-2]\n",
    "            if nums[i]+nums[i-1]>nums[i-2] and a>length:\n",
    "                length=a\n",
    "        return length"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestPerimeter(self, nums: List[int]) -> int:\n",
    "        nums.sort(reverse=True)\n",
    "        for i in range(0, len(nums)-2):\n",
    "            if nums[i] < nums[i+1] + nums[i+2]:\n",
    "                return nums[i] + nums[i+1] +nums[i+2]\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestPerimeter(self, nums: List[int]) -> int:\n",
    "        # 去除多余的xx（超过三个就是没用）\n",
    "        # ds = Counter(nums)\n",
    "        # nns = []\n",
    "        # for k in ds.keys():\n",
    "        #     nns += [k] * min(3, ds[k])\n",
    "        # #\n",
    "        # mx = 0\n",
    "        # for x, y, z in combinations(nns, 3):\n",
    "        #     if (x < y + z) and (y < x + z) and (z < x + y):\n",
    "        #         mx = max(mx, x + y + z)\n",
    "        # return mx\n",
    "\n",
    "        # 去除多余的xx（超过三个就是没用）\n",
    "        # ds = Counter(nums)\n",
    "        # nns = []\n",
    "        # for k in ds.keys():\n",
    "        #     nns += [k] * min(3, ds[k])\n",
    "        # 排序\n",
    "        nns = nums\n",
    "        nns.sort(reverse=True)\n",
    "        for i in range(len(nns) - 2):\n",
    "            if nns[i] < nns[i + 1] + nns[i + 2]:\n",
    "                return nns[i] + nns[i + 1] + nns[i + 2]\n",
    "        return 0\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestPerimeter(self, nums: List[int]) -> int:\n",
    "        nums1 = sorted(nums, reverse=True)\n",
    "        if len(nums) == 3:\n",
    "            if nums1[0] >= nums1[1]+nums1[2]:\n",
    "                return 0\n",
    "            else:\n",
    "                return sum(nums)\n",
    "        else:\n",
    "            i = 0\n",
    "            while i+2 <= len(nums)-1:\n",
    "                if nums1[i] >= nums1[i+1]+nums1[i+2]:\n",
    "                    i += 1\n",
    "                else:\n",
    "                    return nums1[i]+nums1[i+1]+nums1[i+2]\n",
    "            return 0\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestPerimeter(self, nums):\n",
    "        def f(x):\n",
    "            x=sorted(x)\n",
    "            l=len(x)\n",
    "            if l<3:\n",
    "                return 0\n",
    "            if x[l-1]-x[l-2]<x[l-3]:\n",
    "                return x[l-1]+x[l-2]+x[l-3]\n",
    "            else:\n",
    "                x=x[:-1]\n",
    "                return f(x)\n",
    "        return f(nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestPerimeter(self, nums: List[int]) -> int:\n",
    "        nums = sorted(nums,reverse=True)\n",
    "        n=len(nums)\n",
    "        for i in range(n-2):\n",
    "            if nums[i+1]+nums[i+2]>nums[i]:\n",
    "                return nums[i]+nums[i+1]+nums[i+2]\n",
    "        return 0\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestPerimeter(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        for i in range(len(nums)-3, -1, -1):\n",
    "            if nums[i] + nums[i+1] > nums[i+2]:\n",
    "                return nums[i] + nums[i+1] + nums[i+2]\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestPerimeter(self, nums: List[int]) -> int:\n",
    "        nums = sorted(nums, reverse = True)\n",
    "        for i in range(0, len(nums)-2):\n",
    "            if nums[i+1] + nums[i+2] > nums[i]:\n",
    "                return nums[i+1] + nums[i+2] + nums[i]\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestPerimeter(self, nums: List[int]) -> int:\n",
    "        nums.sort(reverse = True)\n",
    "        for i in range(len(nums)-2):\n",
    "            if nums[i] < nums[i+1] + nums[i+2]:\n",
    "                return nums[i] + nums[i+1] + nums[i+2]\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestPerimeter(self, nums: List[int]) -> int:\n",
    "        n=sorted(nums,reverse=True)\n",
    "        for i in range(len(n)-2):\n",
    "            if n[i]<n[i+1]+n[i+2]:\n",
    "                return n[i]+n[i+1]+n[i+2]\n",
    "        return 0\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestPerimeter(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        n, ans = len(nums), 0\n",
    "        for i in range(2, n):\n",
    "            l1, l2, l3 = nums[i], nums[i - 1], nums[i - 2]\n",
    "            if l2 + l3 <= l1: continue\n",
    "            ans = l1 + l2 + l3\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestPerimeter(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        i=len(nums)-3\n",
    "        while i>=0:\n",
    "            if nums[i]+nums[i+1]<=nums[i+2]:\n",
    "                i-=1\n",
    "            else:\n",
    "                return sum(nums[i:i+3])\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestPerimeter(self, nums: List[int]) -> int:\n",
    "        nums=sorted(nums)\n",
    "        for i in range((len(nums))-1,1,-1):\n",
    "            if(nums[i]<nums[i-1]+nums[i-2]):\n",
    "                return nums[i]+nums[i-1]+nums[i-2]\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestPerimeter(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        nums=nums[::-1]\n",
    "        i=0\n",
    "        j=1\n",
    "        k=2\n",
    "        while k<len(nums):\n",
    "            if nums[j]+nums[k]>nums[i]:\n",
    "                return nums[i]+nums[j]+nums[k]\n",
    "            else:\n",
    "                i+=1\n",
    "                j+=1\n",
    "                k+=1\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestPerimeter(self, nums: List[int]) -> int:\n",
    "        # 去除多余的xx（超过三个就是没用）\n",
    "        # ds = Counter(nums)\n",
    "        # nns = []\n",
    "        # for k in ds.keys():\n",
    "        #     nns += [k] * min(3, ds[k])\n",
    "        # #\n",
    "        # mx = 0\n",
    "        # for x, y, z in combinations(nns, 3):\n",
    "        #     if (x < y + z) and (y < x + z) and (z < x + y):\n",
    "        #         mx = max(mx, x + y + z)\n",
    "        # return mx\n",
    "\n",
    "        # 排序\n",
    "        nums.sort(reverse=True)\n",
    "        for i in range(len(nums) - 2):\n",
    "            x, y, z = nums[i], nums[i + 1], nums[i + 2]\n",
    "            if (x < y + z) and (y < x + z) and (z < x + y):\n",
    "                return x + y + z\n",
    "        return 0\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestPerimeter(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        s = 0\n",
    "        arr = []\n",
    "        for i in range(len(nums)-2):\n",
    "            if nums[i]+nums[i+1] > nums[i+2]:\n",
    "                s = nums[i]+nums[i+1]+nums[i+2]\n",
    "                arr.append(s)\n",
    "        if len(arr) == 0:\n",
    "            return 0\n",
    "        else:\n",
    "            return max(arr)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestPerimeter(self, nums: List[int]) -> int:\n",
    "        nums = sorted(nums)\n",
    "        for i in range(2, len(nums))[::-1]:\n",
    "            if nums[i - 1] + nums[i - 2] > nums[i]:\n",
    "                return nums[i] + nums[i - 1] + nums[i - 2]\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestPerimeter(self, A: List[int]) -> int:\n",
    "        A.sort()\n",
    "        m = index = 0\n",
    "        while index < len(A)-2:\n",
    "            if A[index] + A[index+1] > A[index+2]:\n",
    "                m = max(m, A[index]+A[index+1]+A[index+2])\n",
    "            index += 1\n",
    "        return m\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestPerimeter(self, nums: List[int]) -> int:\n",
    "        nums.sort(reverse=True)\n",
    "        i = 0\n",
    "        while i < len(nums) - 2:\n",
    "            if nums[i] + nums[i + 1] > nums[i + 2] and nums[i] + nums[i + 2] > nums[i + 1] \\\n",
    "                    and nums[i + 1] + nums[i + 2] > nums[i]:\n",
    "                return nums[i] + nums[i + 1] + nums[i + 2]\n",
    "            else:\n",
    "                i += 1\n",
    "        return 0\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestPerimeter(self, nums: List[int]) -> int:\n",
    "        nums = sorted(nums)\n",
    "        for i in range(len(nums)-1,1,-1):\n",
    "            if nums[i] < nums[i-1] + nums[i-2]:\n",
    "                return sum(nums[i-2:i+1])\n",
    "        return 0 \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestPerimeter(self, nums: List[int]) -> int:\n",
    "        max0 = len(nums)-1\n",
    "        min1 = len(nums)-2\n",
    "        min2 = len(nums)-3\n",
    "        nums.sort()\n",
    "\n",
    "        while min2>=0:\n",
    "            if nums[min1]+nums[min2]>nums[max0]:\n",
    "                return nums[max0]+nums[min1]+nums[min2]\n",
    "            else:\n",
    "                max0 -= 1\n",
    "                min1 -= 1\n",
    "                min2 -= 1\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestPerimeter(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        last=len(nums)-1\n",
    "        for i in range(2,last+1):\n",
    "            if nums[last]-nums[last-1]>=nums[last-2]:\n",
    "                last-=1\n",
    "            else:\n",
    "                return nums[last]+nums[last-1]+nums[last-2]\n",
    "        return 0\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestPerimeter(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        for i in range(len(nums)-3, -1, -1):\n",
    "            if nums[i] + nums[i+1] > nums[i+2]:\n",
    "                return nums[i] + nums[i+1] + nums[i+2]\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestPerimeter(self, nums: List[int]) -> int:\n",
    "        nums.sort(reverse=True)  \n",
    "        n = len(nums)\n",
    "        for i in range(n - 2):\n",
    "            if nums[i] < nums[i + 1] + nums[i + 2]:\n",
    "                return nums[i] + nums[i + 1] + nums[i + 2]\n",
    "        return 0\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestPerimeter(self, nums: List[int]) -> int:\n",
    "        n = sorted(nums,reverse=True)\n",
    "        for i in range(len(n)-2):\n",
    "            if n[i] < n[i+1] + n[i+2]:\n",
    "                return n[i] + n[i+1] + n[i+2]\n",
    "        return 0\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestPerimeter(self, nums: List[int]) -> int:\n",
    "        n = sorted(nums,reverse=True)\n",
    "        for i in range(len(n)-2):\n",
    "            if n[i] < n[i+1] + n[i+2]:\n",
    "                return n[i] + n[i+1] + n[i+2]\n",
    "        \n",
    "        else:\n",
    "            return 0\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestPerimeter(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        nums.sort(reverse = True)\n",
    "        for i in range(n - 2):\n",
    "            a = nums[i]\n",
    "            b = nums[i + 1]\n",
    "            c = nums[i + 2]\n",
    "            if a < b + c:\n",
    "                return a +  b+ c\n",
    "        return 0\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestPerimeter(self, nums: List[int]) -> int:\n",
    "        m = sorted(nums,reverse=True)\n",
    "        for i in range(len(m)-2):\n",
    "            if m[i] < m[i+1] + m[i+2]:\n",
    "                return m[i] + m[i+1] + m[i+2]\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestPerimeter(self, nums: List[int]) -> int:\n",
    "        nums.sort(reverse=True)\n",
    "        for i in range(1, len(nums)-1):\n",
    "            if nums[i] + nums[i+1] > nums[i-1]:\n",
    "                return nums[i] + nums[i+1] + nums[i-1]\n",
    "        return 0\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestPerimeter(self, nums: List[int]) -> int:\n",
    "        nums=sorted(nums,reverse=True)\n",
    "        for i in range(len(nums)-2):\n",
    "            if nums[i]<nums[i+1]+nums[i+2]:\n",
    "                return nums[i]+nums[i+1]+nums[i+2]\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestPerimeter(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        def judge(a,b,c):\n",
    "            if a+b>c and abs(a-b)<c:\n",
    "                return a+b+c\n",
    "            else:\n",
    "                return 0\n",
    "        while 1:\n",
    "            resule1=judge(nums[len(nums)-1],nums[len(nums)-2],nums[len(nums)-3])\n",
    "            if resule1==0:\n",
    "                nums.remove(nums[len(nums)-1])\n",
    "                if len(nums)<=2:\n",
    "                    return 0\n",
    "            else:\n",
    "                return resule1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestPerimeter(self, nums: List[int]) -> int:\n",
    "        a=sorted(nums)[::-1]\n",
    "        while True:\n",
    "            if len(a)<3:\n",
    "                return 0\n",
    "            elif a[0]<a[1]+a[2]:\n",
    "                return sum(a[0:3])\n",
    "            else:\n",
    "                a.pop(0)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestPerimeter(self, nums: List[int]) -> int:\n",
    "        # 排序后，最大的一条边与相邻的两个边若都无法组成三角形，那么后面的都不行\n",
    "        nums.sort(reverse = True)\n",
    "        a = 0\n",
    "\n",
    "        while a+2<=len(nums)-1:\n",
    "    \n",
    "            if nums[a] >= nums[a+1] + nums[a+2]:\n",
    "                a += 1\n",
    "            else:\n",
    "                return nums[a] + nums[a+1] + nums[a+2]\n",
    "\n",
    "        return 0 \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestPerimeter(self, nums: List[int]) -> int:\n",
    "        nums.sort(reverse=True)\n",
    "        C=0\n",
    "        len_=len(nums)\n",
    "        for i in range(len_-2):\n",
    "            if nums[i]<nums[i+1]+nums[i+2] and nums[i]+nums[i+1]+nums[i+2]>C:\n",
    "                return nums[i]+nums[i+1]+nums[i+2]\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestPerimeter(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        nums.sort()\n",
    "        for i in range(n-1, 1, -1):\n",
    "            if nums[i] < nums[i-1] + nums[i-2]:\n",
    "                return nums[i] + nums[i-1] + nums[i-2]\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestPerimeter(self, nums: List[int]) -> int:\n",
    "        nums = sorted(nums)\n",
    "        for i in reversed(range(len(nums) - 2)):\n",
    "            if nums[i + 2] < nums[i + 1] + nums[i]:\n",
    "                return nums[i + 1] + nums[i] + nums[i + 2]\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestPerimeter(self, nums: List[int]) -> int:\n",
    "        n = sorted(nums,reverse=True)\n",
    "        l = len(n)\n",
    "        for i in range(l-2):\n",
    "            if n[i]<n[i+1]+n[i+2]:\n",
    "                return n[i]+n[i+1]+n[i+2]\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestPerimeter(self, A: List[int]) -> int:\n",
    "        A.sort()\n",
    "        m = index = 0\n",
    "        while index < len(A)-2:\n",
    "            if A[index] + A[index+1] > A[index+2]:\n",
    "                m = max(m, A[index]+A[index+1]+A[index+2])\n",
    "            index += 1\n",
    "        return m\n",
    "\n",
    "                \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestPerimeter(self, nums: List[int]) -> int:\n",
    "        nums.sort(reverse = True)\n",
    "        if len(nums) == 3:\n",
    "            if nums[0] >= nums[1] + nums[2]:\n",
    "                return 0\n",
    "            else:\n",
    "                return sum(nums)\n",
    "        else:\n",
    "            i = 0\n",
    "            while i + 2 <= len(nums) - 1:\n",
    "                if nums[i] >= nums[i+1] + nums[i+2]:\n",
    "                    i += 1\n",
    "                else:\n",
    "                    return nums[i] + nums[i+1] + nums[i+2]\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestPerimeter(self, nums: List[int]) -> int:\n",
    "        n = sorted(nums,reverse=True)\n",
    "        for i in range(len(n)-2):\n",
    "            if n[i] < n[i+1]+n[i+2]:\n",
    "                return n[i] + n[i+1] +n[i+2]\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestPerimeter(self, nums: List[int]) -> int:\n",
    "        nums = sorted(nums)[::-1]\n",
    "        n = len(nums)\n",
    "        l1 = 0\n",
    "        while l1+2 < n and nums[l1] >= nums[l1+1] + nums[l1+2]:\n",
    "            l1 += 1\n",
    "        return nums[l1] + nums[l1+1] + nums[l1+2] if l1+2 < n else 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestPerimeter(self, nums: List[int]) -> int:\n",
    "        n = sorted(nums,reverse=True)\n",
    "        for i in range(len(n)-2):\n",
    "            if n[i] < n[i+1] + n[i+2]:\n",
    "                return n[i] + n[i+1] + n[i+2]\n",
    "        return 0\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestPerimeter(self, nums: List[int]) -> int:\n",
    "        s = sorted(nums, reverse=True)\n",
    "        for i in range(len(s)-2):\n",
    "            if s[i+1] + s[i+2] > s[i]:\n",
    "                return s[i] + s[i+1] + s[i+2]\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestPerimeter(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        i = len(nums) - 1\n",
    "        while i >= 2:\n",
    "            if nums[i-2] + nums[i-1] <= nums[i]:\n",
    "                i-=1\n",
    "            else:\n",
    "                return sum(nums[i-2:i+1]) \n",
    "        return 0\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestPerimeter(self, nums: List[int]) -> int:\n",
    "        def isTriangle(l):\n",
    "            l = sorted(l)\n",
    "            if l[0] + l[1] > l[2]:\n",
    "                return True\n",
    "            return False\n",
    "\n",
    "        nums = sorted(nums)\n",
    "        index = len(nums)-1\n",
    "        while index>=2:\n",
    "            if isTriangle(nums[index-2:index+1]):\n",
    "                return sum(nums[index-2:index+1])\n",
    "            index -= 1\n",
    "        return 0\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestPerimeter(self, nums: List[int]) -> int:\n",
    "        nums = sorted(nums, reverse=True)\n",
    "        i = 0\n",
    "        while i + 2 < len(nums):\n",
    "            if nums[i + 1] + nums[i + 2] > nums[i]:\n",
    "                return nums[i + 1] + nums[i + 2] + nums[i]\n",
    "            i += 1\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestPerimeter(self, nums: List[int]) -> int:\n",
    "        # 去除多余的xx（超过三个就是没用）\n",
    "        # ds = Counter(nums)\n",
    "        # nns = []\n",
    "        # for k in ds.keys():\n",
    "        #     nns += [k] * min(3, ds[k])\n",
    "        # #\n",
    "        # mx = 0\n",
    "        # for x, y, z in combinations(nns, 3):\n",
    "        #     if (x < y + z) and (y < x + z) and (z < x + y):\n",
    "        #         mx = max(mx, x + y + z)\n",
    "        # return mx\n",
    "\n",
    "        # 去除多余的xx（超过三个就是没用）\n",
    "        # ds = Counter(nums)\n",
    "        # nns = []\n",
    "        # for k in ds.keys():\n",
    "        #     nns += [k] * min(3, ds[k])\n",
    "        # 排序\n",
    "        nns = nums\n",
    "        nns.sort(reverse=True)\n",
    "        for i in range(len(nns) - 2):\n",
    "            x, y, z = nns[i], nns[i + 1], nns[i + 2]\n",
    "            if (x < y + z) and (y < x + z) and (z < x + y):\n",
    "                return x + y + z\n",
    "        return 0\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestPerimeter(self, A: List[int]) -> int:\n",
    "        A.sort(reverse=True)\n",
    "        for i in range(len(A)-2):\n",
    "            if  A[i]<A[i+1]+A[i+2]:\n",
    "                return A[i]+A[i+1]+A[i+2]\n",
    "        return 0\n",
    "        \n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isTriangle(self, nums: List[int]) -> bool:\n",
    "        a, b, c = nums[:3]  # 最大的三条边\n",
    "        if b + c > a and a - b < c:\n",
    "            return True\n",
    "        else:\n",
    "            return False\n",
    "\n",
    "    def largestPerimeter(self, nums: List[int]) -> int:\n",
    "        nums = sorted(nums, reverse=True)\n",
    "        while len(nums) >= 3:\n",
    "            if self.isTriangle(nums):\n",
    "                return sum(nums[:3])\n",
    "            else:\n",
    "                nums.pop(0)\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestPerimeter(self, nums: List[int]) -> int:\n",
    "        nums.sort(reverse = True)\n",
    "        for i in range(len(nums) - 2):\n",
    "            if nums[i] < nums[i + 1] + nums[i + 2]:\n",
    "                return nums[i] + nums[i + 1] + nums[i + 2]\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestPerimeter(self, nums: List[int]) -> int:\n",
    "        nums.sort(reverse=True)\n",
    "        if len(nums) == 3:\n",
    "            if nums[0] >= nums[1]+nums[2]:\n",
    "                return 0\n",
    "            else:\n",
    "                return sum(nums)\n",
    "        else:\n",
    "            i = 0\n",
    "            while i+2 <= len(nums)-1:\n",
    "                if nums[i] >= nums[i+1]+nums[i+2]:\n",
    "                    i += 1\n",
    "                else:\n",
    "                    return nums[i]+nums[i+1]+nums[i+2]\n",
    "            return 0\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestPerimeter(self, nums: List[int]) -> int:\n",
    "        nums = sorted(nums)\n",
    "        for i in range(len(nums) - 1, 1, -1):\n",
    "            if (nums[i-2] + nums[i-1] > nums[i]):\n",
    "                return nums[i-2] + nums[i-1] + nums[i]\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestPerimeter(self, nums: List[int]) -> int:\n",
    "        nums = sorted(nums,reverse=True)\n",
    "        for i in range(len(nums) - 2):\n",
    "            if nums[i] < nums[i + 1] + nums[i + 2]:\n",
    "                return nums[i] + nums[i + 1] + nums[i + 2]\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def largestPerimeter(self, nums: List[int]) -> int:\r\n",
    "        n = len(nums)\r\n",
    "        nums.sort()\r\n",
    "        for i in reversed(range(n)):\r\n",
    "            for j in reversed(range(1, i)):\r\n",
    "                k = j-1\r\n",
    "                if nums[i] < nums[j]+nums[k]:\r\n",
    "                    return nums[i]+nums[j]+nums[k]\r\n",
    "        return 0\r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestPerimeter(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        for i in range(len(nums)-1,1,-1):\n",
    "            if nums[i]<nums[i-1]+nums[i-2]:\n",
    "                return nums[i]+nums[i-1]+nums[i-2]\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestPerimeter(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        for i in range(len(nums)-1, 1, -1):\n",
    "            if nums[i] < nums[i-1] + nums[i-2]:\n",
    "                return nums[i] + nums[i-1] + nums[i-2]\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestPerimeter(self, nums: List[int]) -> int:\n",
    "        nums.sort(reverse=True)\n",
    "        ans=0\n",
    "        i=0\n",
    "        while i<len(nums)-2:\n",
    "            if nums[i]<nums[i+1]+nums[i+2]:\n",
    "                ans=nums[i]+nums[i+1]+nums[i+2]\n",
    "                break\n",
    "            i+=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestPerimeter(self, nums: List[int]) -> int:\n",
    "        a = sorted(nums, reverse=True)\n",
    "        n = len(a)\n",
    "        for i in range(n-2):\n",
    "            if a[i+2] + a[i+1] > a[i]:\n",
    "                return sum(a[i:i+3])\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def largestPerimeter(self, nums: List[int]) -> int:\r\n",
    "        n = len(nums)\r\n",
    "        nums.sort()\r\n",
    "        for i in reversed(range(2, n)):\r\n",
    "            j = i-1\r\n",
    "            k = j-1\r\n",
    "            if nums[i] < nums[j]+nums[k]:\r\n",
    "                return nums[i]+nums[j]+nums[k]\r\n",
    "        return 0\r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestPerimeter(self, A: List[int]) -> int:\n",
    "        A.sort(reverse=True)\n",
    "        return next((x+y+z for x,y,z in zip(A,A[1:],A[2:]) if x<y+z),0)\n",
    "        \n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestPerimeter(self, nums: List[int]) -> int:\n",
    "        nums.sort(reverse=True)\n",
    "        print(nums)\n",
    "        i=0\n",
    "        while i< len(nums)-2:\n",
    "            if nums[i] < nums[i+1]  + nums[i+2]:\n",
    "                return nums[i]+ nums[i+1] + nums[i+2]\n",
    "            i=i+1\n",
    "        if i== len(nums)-2:\n",
    "            return 0 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestPerimeter(self, nums: List[int]) -> int:\n",
    "        num1=sorted(nums,reverse=True)\n",
    "        m=0\n",
    "        c=len(num1)\n",
    "        while c>=3:\n",
    "            if num1[m+1]+num1[m+2]>num1[m]:\n",
    "                return num1[m]+num1[m+1]+num1[m+2]\n",
    "            else:\n",
    "                m+=1\n",
    "                c-=1\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestPerimeter(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        for i in range(len(nums)-1,1,-1):\n",
    "            if nums[i-2]+nums[i-1]>nums[i]:\n",
    "                return nums[i-2]+nums[i-1]+nums[i]  \n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestPerimeter(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        nums.sort(reverse=True)\n",
    "        for i in range(n - 2):\n",
    "            if nums[i] < nums[i + 1] + nums[i + 2]:\n",
    "                return nums[i] + nums[i + 1] + nums[i + 2]\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestPerimeter(self, nums: List[int]) -> int:\n",
    "        nums.sort(key=lambda x:-x)\n",
    "        l = len(nums)\n",
    "        result = -inf\n",
    "        for i in range(l-2):\n",
    "            left,right = i+1,i+2\n",
    "            if nums[left]+nums[right]+nums[i]<result:\n",
    "                break\n",
    "            if nums[i]<(nums[left]+nums[right]):\n",
    "                result = max(result,nums[i]+nums[left]+nums[right])\n",
    "        return 0 if result==-inf else result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestPerimeter(self, nums: List[int]) -> int: \n",
    "        nums=sorted(nums,reverse=True)\n",
    "        for i in range(len(nums)-2):\n",
    "            if nums[i]>=nums[i+1]+nums[i+2]:\n",
    "                continue  \n",
    "            else:\n",
    "                return nums[i]+nums[i+1]+nums[i+2]\n",
    "        return 0\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestPerimeter(self, nums: List[int]) -> int:\n",
    "        nums1 = sorted(nums, reverse=True)\n",
    "        if len(nums) == 3:\n",
    "            if nums1[0] >= nums1[1]+nums1[2]:\n",
    "                return 0\n",
    "            else:\n",
    "                return sum(nums)\n",
    "        else:\n",
    "            i = 0\n",
    "            while i+2 <= len(nums)-1:\n",
    "                if nums1[i] >= nums1[i+1]+nums1[i+2]:\n",
    "                    i += 1\n",
    "                else:\n",
    "                    return nums1[i]+nums1[i+1]+nums1[i+2]\n",
    "            return 0"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
